﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_DairyProduct
    {
        public bool CreateOneDairyProduct(DairyProduct_Info objDairyProduct_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("DairyProduct_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", objDairyProduct_Info.DairyProductID));
            command.Parameters.Add(new SqlParameter(@"DairyProductName", objDairyProduct_Info.DairyProductName));
            command.Parameters.Add(new SqlParameter(@"Description", objDairyProduct_Info.Description));
            command.Parameters.Add(new SqlParameter(@"FarmID", objDairyProduct_Info.FarmID));
            command.Parameters.Add(new SqlParameter(@"MilkTreatmentID", objDairyProduct_Info.MilkTreatmentID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneDairyProduct(DairyProduct_Info objDairyProduct_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("DairyProduct_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DairyProductID", objDairyProduct_Info.DairyProductID));
            command.Parameters.Add(new SqlParameter("@DairyProductName", objDairyProduct_Info.DairyProductName));
            command.Parameters.Add(new SqlParameter("@Description", objDairyProduct_Info.Description));
            command.Parameters.Add(new SqlParameter("@FarmID", objDairyProduct_Info.FarmID));
            command.Parameters.Add(new SqlParameter("@MilkTreatmentID", objDairyProduct_Info.MilkTreatmentID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneDairyProduct(int DairyProductID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("DairyProduct_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", DairyProductID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllDairyProduct()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("DairyProduct_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public DairyProduct_Info GetOneDairyProduct(int DairyProductID)
        {
            DairyProduct_Info objDairyProduct_Info = new DairyProduct_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("DairyProduct_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"DairyProductID", DairyProductID));
            try
            {
                objConnection.Open();
                SqlDataReader ordDairyProduct = command.ExecuteReader(); //OjectRecordDatareader
                ordDairyProduct.Read();
                if (ordDairyProduct.HasRows)
                {
                    if (ordDairyProduct["DairyProductID"] != DBNull.Value)
                    {
                        objDairyProduct_Info.DairyProductID = Convert.ToInt16(ordDairyProduct["DairyProductID"]);
                    }
                    if (ordDairyProduct["DairyProductName"] != DBNull.Value)
                    {
                        objDairyProduct_Info.DairyProductName = Convert.ToString(ordDairyProduct["DairyProductName"]);
                    }
                    if (ordDairyProduct["Description"] != DBNull.Value)
                    {
                        objDairyProduct_Info.Description = Convert.ToString(ordDairyProduct["Description"]);
                    }
                    if (ordDairyProduct["FarmID"] != DBNull.Value)
                    {
                        objDairyProduct_Info.FarmID = Convert.ToInt16(ordDairyProduct["FarmID"]);
                    }
                    if (ordDairyProduct["MilkTreatmentID"] != DBNull.Value)
                    {
                        objDairyProduct_Info.MilkTreatmentID = Convert.ToInt16(ordDairyProduct["MilkTreatmentID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objDairyProduct_Info;
        }
    }
}
